/**
 * 				MochitoSockedClient.java
 *				------------
 *		
 *   Author:	bin04017
 *   ID:		bin04017
 *	 Created:	24.01.2007
 *	 Project:	Mochito-DataHandler
 *	 Package:	mochito.clientSide
 *	 Usage:		
 */
package mochito.clientSide;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import mochito.MochitoException;
import mochito.tranferFactory.MochitoHost;

/**
 * @author bin04017
 * 
 */
public class MochitoSockedClient {

	private MochitoHost host;

	private Socket clientSocket;

	private PrintWriter out = null;

	private BufferedReader in = null;

	public MochitoSockedClient(MochitoHost host) throws MochitoException {
		this.host = host;
		init();
	}

	private void init() throws MochitoException {

		try {
			clientSocket = new Socket(host.getHostName(), host.getPort());
			out = new PrintWriter(clientSocket.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(clientSocket
					.getInputStream()));
		} catch (Exception e) {
			throw new MochitoException(e);
		}

		String fromServer;

		try {
			while ((fromServer = in.readLine()) != null) {
				System.out.println("Server: " + fromServer);
				
			}
		} catch (IOException e) {
			throw new MochitoException(e);
		}
	}

	public void close() throws MochitoException {
		out.close();
		try {
			in.close();
		} catch (IOException e) {
			throw new MochitoException(e);
		}
	}

}
